using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class Player : MonoBehaviour
{
    [Header("移动设置")]
    [SerializeField] private float moveSpeed=5f; 
    [SerializeField] private float acceleration=50f;
    [SerializeField] private float deceleration=50f;
    private  Vector2  moveDirection;
    private Vector2 currentVelocity;
    private Rigidbody2D rb;
    private void Awake()
    {
        rb=GetComponent<Rigidbody2D>();
        
    }
    private void Update()
    {
        moveDirection.x = Input.GetAxisRaw("Horizontal");
        moveDirection.y = Input.GetAxisRaw("Vertical");
        if (moveDirection.magnitude > 1f)
        {
            moveDirection.Normalize();
        }

    }
    
    private void FixedUpdate()
    {
        // 计算目标速度
        Vector2 targetVelocity = moveDirection * moveSpeed;
        
        // 平滑处理速度变化
        currentVelocity = Vector2.MoveTowards(
            currentVelocity,
            targetVelocity,
            (moveDirection.magnitude > 0 ? acceleration : deceleration) * Time.fixedDeltaTime
        );
        
        // 应用移动
        rb.velocity = currentVelocity;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            print("碰撞到了敌人");
            Destroy(collision.gameObject);
        }
    }
   
   
}